package com.sense.kukoo.biz_media.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.sense.kukoo.biz_media.adapter.MediaChannelAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.repo.biz.ChannelContentBean
import com.sense.kukoo.common.repo.http.bean.UserInfoBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class MediaChannelTitleProvider(
    val listener: MediaChannelAdapter.IndexContentEventListener
): MultiItemProvider<ChannelContentBean>() {

    private var mCurrentUserId = 0L

    override fun getItemType(): Int = ChannelContentBean.TYPE_TITLE

    override fun getItemLayoutId(): Int = R.layout.common_item_channel_title

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: ChannelContentBean) {
        val data: UserInfoBean = item.user!!
        //
        val iv_me_header_icon = viewHolder.getView(R.id.iv_me_header_icon) as ImageView
        Monet.get(iv_me_header_icon.context)
            .load<GlideConfig>(data.getIconUrl())
            .placeholder(com.sense.kukoo.common_design.R.drawable.design_icon_avatar_default)
            .circleCrop()
            .into(iv_me_header_icon)
        //
        val tv_me_channel = viewHolder.getView(R.id.tv_me_channel) as TextView
        tv_me_channel.text = data.getName()
        //
        val tv_me_id = viewHolder.getView(R.id.tv_me_id) as TextView
        tv_me_id.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.num_id, data.id)
        tv_me_id.setOnClickListener {
            listener.onChannelUserIdClick(data)
        }
        //
        val tv_me_followers = viewHolder.getView(R.id.tv_me_followers) as TextView
        tv_me_followers.text = "${data.fansNum}"
        //
        val tv_me_following = viewHolder.getView(R.id.tv_me_following) as TextView
        tv_me_following.text = "${data.subscriptionNum}"
        //
        val tv_me_likes = viewHolder.getView(R.id.tv_me_likes) as TextView
        tv_me_likes.text = "${data.likeNum}"
        //
        val tv_me_listened = viewHolder.getView(R.id.tv_me_listened) as TextView
        var min = data.listenedTimes / 60
        val hour = min / 60
        if(hour > 0){
            min = min % 60
            tv_me_listened.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.time_formate_min_and_hour, hour, min)
        } else {
            tv_me_listened.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.time_formate_min, min)
        }
        //
        val tv_btn_follow = viewHolder.getView(R.id.tv_btn_follow) as TextView
        if(data.id == mCurrentUserId){
            tv_btn_follow.visibility = View.GONE
        } else {
            tv_btn_follow.visibility = View.VISIBLE
            //
            if (item.isFollow) {
                tv_btn_follow.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.following)
                tv_btn_follow.setTextColor(tv_btn_follow.resources.getColor(com.sense.kukoo.common_design.R.color.text_active))
                tv_btn_follow.setBackgroundResource(com.sense.kukoo.common_design.R.drawable.design_bg_btn_follow_active)
            } else {
                tv_btn_follow.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.follow)
                tv_btn_follow.setTextColor(tv_btn_follow.resources.getColor(com.sense.kukoo.common_design.R.color.text_white))
                tv_btn_follow.setBackgroundResource(com.sense.kukoo.common_design.R.drawable.design_bg_btn_follow_def)
            }
            tv_btn_follow.setOnClickListener {
                listener.onFollowClick(data)
            }
        }
    }

    fun setCurrentUserId(currentUserId:Long){
        mCurrentUserId = currentUserId
    }
}